package com.cloud.system.api.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.system.api.bean.SysDictForm;
import com.cloud.system.api.entity.SysDict;
import com.cloud.system.api.entity.SysDictItem;
import com.cloud.system.api.mapper.SysDictItemMapper;
import com.cloud.system.api.mapper.SysDictMapper;
import com.cloud.system.api.service.SysDictService;
import com.cloud.system.api.service.SystemCacheService;
import com.cloud.system.common.exception.BizException;
import com.cloud.system.common.utils.RedisKeyHelper;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class SysDictServiceImpl implements SysDictService {

	@Autowired
	private SysDictMapper sysDictMapper;

	@Autowired
	private SysDictItemMapper sysDictItemMapper;

	@Autowired
	private SystemCacheService systemCacheService;

	@Override
	public List<SysDict> list() {
		Example example = new Example(SysDict.class);
		example.orderBy("dictKey");
		return sysDictMapper.selectByExample(example);
	}

	@Override
	public SysDictForm saveUpdateDict(SysDictForm form) throws BizException {
		Integer dictId = form.getDictId();
		if (null != dictId && dictId > 0) {
			SysDict dict = new SysDict();
			BeanUtils.copyProperties(form, dict);
			sysDictMapper.updateByPrimaryKey(dict);
			Example example = new Example(SysDictItem.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("dictId", dictId);
			sysDictItemMapper.deleteByExample(example);
		} else {

			SysDict dict = new SysDict();
			BeanUtils.copyProperties(form, dict);
		
			sysDictMapper.insert(dict);
		}
		List<SysDictItem> items = form.getItems();
		for (SysDictItem item : items) {
			item.setDictId(dictId);
			item.setDictKey(form.getDictKey());
			sysDictItemMapper.insert(item);
		}
		systemCacheService.evitDictCache(form.getDictKey());
		return form;
	}

	@Override
	public SysDict deleteSysDict(SysDict dict) {
		Example dictItemExample = new Example(SysDictItem.class);
		Example.Criteria dictItemCriteria = dictItemExample.createCriteria();
		dictItemCriteria.andEqualTo("dictKey", dict.getDictKey());
		sysDictItemMapper.deleteByExample(dictItemExample);
		sysDictMapper.deleteByPrimaryKey(dict);
		systemCacheService.evitDictCache(dict.getDictKey());
		return dict;
	}

	@Override
	public List<SysDictItem> loadDictByDictItemValue(String dictItemValue, String dicKey) {
		Example example = new Example(SysDictItem.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("dictItemValue", dictItemValue);
		criteria.andEqualTo("dictKey", dicKey);
		example.orderBy("displayOrder").asc();
		return sysDictItemMapper.selectByExample(example);
	}

	@Override
	public SysDictForm loadDictDetail(int dictId) {
		if (dictId > 0) {
			SysDict dict = sysDictMapper.selectByPrimaryKey(dictId);
			SysDictForm form = new SysDictForm();
			BeanUtils.copyProperties(dict, form);
			Example example = new Example(SysDictItem.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("dictId", dictId);
			example.orderBy("displayOrder").asc();
			List<SysDictItem> items = sysDictItemMapper.selectByExample(example);
			form.setItems(items);
			return form;
		} else {
			SysDictForm form = new SysDictForm();
			return form;
		}
	}

}
